package com.wbs.shop.nuby.api;

import android.content.Context;
import android.content.DialogInterface;

import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.wbs.shop.nuby.api.exception.FirmException;
import com.wbs.shop.nuby.api.exception.NullDataException;
import com.wbs.shop.nuby.bean.ResultBean;
import com.wbs.shop.nuby.view.listener.RefreshListener;
import com.wbs.shop.nuby.view.listener.SmartRefreshListener;
import com.wbs.shop.nuby.utils.Logger;
import com.wbs.shop.nuby.utils.StringUtils;
import com.wbs.shop.nuby.utils.Utils;
import com.wbs.shop.nuby.view.dialog.LoadingDialog;

import io.reactivex.observers.ResourceObserver;


/**
 * Created by Jam on 16-7-21
 * Description: 自定义Subscribe
 */
public abstract class RxSubscribe<T> extends ResourceObserver<T> implements DialogInterface.OnDismissListener {
    private Context mContext;
    private LoadingDialog dialog;
    private boolean mCancelable = true;
    private boolean isDismiss = false;
    /**
     * 是否显示提示消息
     */
    private boolean showMsg = true;
    /**
     * 是否显示加载动画
     */
    private boolean showLoadingDialog = true;
    /**
     * 于刷新listview配合使用
     */
    private RefreshListener refreshListener;

    public RxSubscribe<T> smartRefreshListener(RefreshLayout refreshLayout) {
        return refreshListener(new SmartRefreshListener(refreshLayout));
    }

    public RxSubscribe<T> refreshListener(RefreshListener refreshListener) {
        this.refreshListener = refreshListener;
        return this;
    }

    public RxSubscribe<T> showLoadingDialog(boolean showLoadingDialog) {
        this.showLoadingDialog = showLoadingDialog;
        return this;
    }

    public RxSubscribe<T> showMsg(boolean showMsg) {
        this.showMsg = showMsg;
        return this;
    }

    public RxSubscribe<T> setCancelable(boolean flag) {
        mCancelable = flag;
        return this;
    }

    protected boolean showDialog() {
        return showLoadingDialog;
    }

    /**
     * @param context context
     */
    public RxSubscribe(Context context) {
        this.mContext = context;
    }

    public RxSubscribe() {

    }

    @Override
    public void onComplete() {
        if (showDialog())
            dismiss();
    }

    @Override
    public void onStart() {
        super.onStart();
        if (mContext != null && showDialog()) {
            try {
                dialog = new LoadingDialog(mContext);
                dialog.setCancelable(mCancelable);
                dialog.setOnDismissListener(this);
                dialog.show();
            } catch (Exception e) {
                Logger.e(e);
            }
        }
    }

    @Override
    public void onNext(T t) {
        try {
            _onNext(t);
            dismiss();
        } catch (Exception e) {
            onError(e);
        }
    }

    @Override
    public void onError(Throwable e) {
        if (e instanceof NullDataException) {
            onNext(null);
            return;
        }
        try {
            if (showMsg) {
                if (e instanceof FirmException) {
                    if (!doFirmError((FirmException) e) && StringUtils.isNotEmpty(e.getMessage())) {
                        Utils.toastMessageLong(e.getMessage());
                    }
                } else {
                    Utils.toastMessageLong("请求异常");
                }
            }
            if (e instanceof FirmException) {
                FirmException e1 = (FirmException) e;
                if (ResultBean.NOT_LOGIN == e1.getCode() && mContext != null) {
//                    Intent intent = new Intent(mContext, LoginActivity.class);
//                    mContext.startActivity(intent);
                }
            }
            Logger.e(e);
            doError(e);
        } catch (Exception e1) {
            Logger.e(e1);
        }
        if (showDialog())
            dismiss();
    }

    /**
     * @param e
     * @return 是否已被处理
     */
    protected boolean doFirmError(FirmException e) {
        return false;
    }

    protected void doError(Throwable e) {

    }

    private void dismiss() {
        try {
            if (refreshListener != null) {
                refreshListener.stopRefresh();
            }
            if (dialog != null) {
                dialog.dismiss();
            }
            isDismiss = true;
            onDismiss();
        } catch (Exception e) {
            Logger.e(e);
        }
    }

    public void onDismiss(DialogInterface dialog) {
        if (!isDismiss && !isDisposed()) {// 没有请求完则断开请求
            //dispose();
        }
    }

    public void onDismiss() {

    }

    protected abstract void _onNext(T t);

}
